package com.etone.smartAudit.utils.database;

import com.etone.smartAudit.domain.DataMeta;
import com.etone.smartAudit.enums.DataBaseType;
import com.etone.smartAudit.utils.ArraysExtendUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: sovy
 * @date: 2020/11/19
 * @description:
 */
@Log4j2
public class GreenplumUtil {

    /**
     * @param ip
     * @param userName
     * @param password
     * @param port
     * @param database
     * @param schema
     * @param tableName
     * @param dataMetas
     */
    public static void createTable(String ip, String userName, String password, Integer port, String database, String schema, String tableName, List<DataMeta> dataMetas) {
        String schemaTable = "\"" + schema + "\".\"" + tableName + "\"";
        String createTableSql = DatabaseUtil.splitSpace("CREATE", "TABLE", schemaTable, "(");
        List<String> sbs = new ArrayList<>();
        for (DataMeta dataMeta : dataMetas) {
            String columnSql = dataMeta.getFieldName();
            sbs.add(columnSql);
        }
        //字段
        String fields = dataMetas.stream().map(dataMeta -> {
            String columnSql = DatabaseUtil.splitSpace("\"" + dataMeta.getFieldName() + "\"", dataMeta.getType());
            if (dataMeta.getNullable() != null && !dataMeta.getNullable()) {
                columnSql += DatabaseUtil.splitSpace("NOT", "NULL");
            }
            if (dataMeta.getDefaultValue() != null) {
                columnSql += DatabaseUtil.splitSpace("DEFAULT", dataMeta.getDefaultValue());
            }
            return columnSql;
        }).collect(Collectors.joining(","));

        //主键
        String primaryKeys = dataMetas.stream().filter(dataMeta -> dataMeta.getPrimaryKey() != null && dataMeta.getPrimaryKey()).map(dataMeta -> dataMeta.getFieldName()).collect(Collectors.joining(","));
        if (!StringUtils.isEmpty(primaryKeys)) {
            primaryKeys = DatabaseUtil.splitSpace(",", "PRIMARY", "KEY", "(", primaryKeys, ")");
        } else {
            primaryKeys = "";
        }

        //唯一约束
        String uniqueKeys = dataMetas.stream().filter(dataMeta -> dataMeta.getUniqueKey() != null && dataMeta.getUniqueKey()).map(dataMeta -> dataMeta.getFieldName()).collect(Collectors.joining(","));
        if (!StringUtils.isEmpty(uniqueKeys)) {
            uniqueKeys = DatabaseUtil.splitSpace(",", "UNIQUE", "INDEX", "(", uniqueKeys, ")");
        } else {
            uniqueKeys = "";
        }
        createTableSql = DatabaseUtil.splitSpace(createTableSql, fields, primaryKeys, uniqueKeys, ")");
        String authTable = DatabaseUtil.splitSpace("ALTER", "TABLE", schema + "." + tableName, " OWNER", "TO", userName);

        //字段说明
        List<String> commentSqls = dataMetas.stream().filter(dataMeta -> dataMeta.getComment() != null).map(dataMeta -> DatabaseUtil.splitSpace("COMMENT", "ON", "COLUMN", schemaTable + ".\"" + dataMeta.getFieldName() + "\"", "IS", "'" + dataMeta.getComment() + "'")).collect(Collectors.toList());

        DatabaseUtil.execSql(ip, userName, password, port, database, ArraysExtendUtil.mergerArraysList(Arrays.asList(createTableSql, authTable), commentSqls), DataBaseType.GREENPLUM);
    }

    /**
     * 表授权
     *
     * @param ip
     * @param userName
     * @param password
     * @param port
     * @param database
     * @param schema
     * @param tableName
     * @param toUserName
     */
    public static void authTableSelect(String ip, String userName, String password, Integer port, String database, String schema, String tableName, String toUserName) {

        String authSchemaUsage = "GRANT Usage ON SCHEMA " + schema + " TO " + toUserName;
        String authTable = "GRANT Select ON TABLE " + schema + "." + tableName + " TO " + toUserName;

        DatabaseUtil.execSql(ip, userName, password, port, database, Arrays.asList(authSchemaUsage, authTable), DataBaseType.GREENPLUM);
    }

    /**
     * 删除表
     *
     * @param ip
     * @param userName
     * @param password
     * @param port
     * @param database
     * @param schema
     * @param tableName
     */
    public static void deleteTable(String ip, String userName, String password, Integer port, String database, String schema, String tableName) {
        String deleteSql = "drop table " + tableName;
        DatabaseUtil.execSql(ip, userName, password, port, database, Arrays.asList(deleteSql), DataBaseType.GREENPLUM);
    }
}
